package com.qdaily.ui.ui.fragment;

import android.os.Handler;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.ViewStub;
import com.qdaily.supportlibrary.request.JsonData;
import com.qdaily.supportlibrary.util.NetUtil;
import com.qdaily.supportlibrary.util.QDJsonUtil;
import com.qdaily.ui.QDBaseFragment;
import com.qdaily.ui.R;
import com.qdaily.ui.data.FragPagerAdapter;
import com.qdaily.ui.net.NetConfigs;
import com.qdaily.ui.net.entity.SendCommentEntity;
import com.qdaily.ui.net.model.MyCommentMeta;
import com.qdaily.ui.ui.views.InputBar;
import com.qdaily.ui.ui.views.PagerSlidingTabStrip;
import com.qdaily.ui.ui.views.TitleBar;
import com.squareup.otto.Subscribe;

/**
 * 我的动态
 * Created with IntelliJ IDEA.
 * User: Felix
 * Date: 4/23/15
 * Time: 00:53
 * QQ:74104
 * Email:lovejiuwei@gmail.com
 */
public class MyDynamicFragment extends QDBaseFragment implements InputBar.InputBarInterface,
        InputBar.InputBarKeyboardInterface {
    private com.qdaily.ui.ui.views.PagerSlidingTabStrip pagerSlidingTabStrip;
    private android.support.v4.view.ViewPager viewPagerMyDynamic;
    private TitleBar viewTitleHeaderBar;
    private InputBar inputBar;
    private boolean isInputBarInit = false;
    private View rootView;

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_my_dynamic;
    }

    @Override
    protected void setupViews(View view) {
        this.rootView = view;
        this.viewPagerMyDynamic = (ViewPager) view.findViewById(R.id.viewPagerMyDynamic);
        this.pagerSlidingTabStrip = (PagerSlidingTabStrip) view.findViewById(R.id.pagerSlidingTabStrip);

        this.viewTitleHeaderBar = (TitleBar) view.findViewById(R.id.viewTitleHeaderBar);
        this.viewTitleHeaderBar.setTitle(R.string.my_dyna);
        this.viewTitleHeaderBar.getImageViewTitleBarLeft().setVisibility(View.INVISIBLE);
        this.viewPagerMyDynamic.setOffscreenPageLimit(1);


        this.viewPagerMyDynamic.setAdapter(new FragPagerAdapter(getChildFragmentManager(),
                new QDBaseFragment[]{
                        new MyCommentsFragment(),
                        new MyLikedFragment(),
                        new MyJoinMobFragment()}));

        this.pagerSlidingTabStrip.setViewPager(viewPagerMyDynamic);
    }

    @Override
    protected void setupData() {

    }

    @Subscribe
    public void reveiveOnReplyClick(MyCommentMeta itemData) {
        if (inputBar == null) {
            ViewStub stub = (ViewStub) rootView.findViewById(R.id.viewstub_inputbar);
            stub.inflate();
            inputBar = new InputBar(getContext(), rootView, R.id.my_dynamic_inputbar, false);
            inputBar.configMinInputbar();
            inputBar.setBarInterface(this);
            inputBar.setBarKeyboardInterface(this);

            Handler handler = new Handler();
            handler.postDelayed(new Runnable() {
                @Override
                public void run() {
                    inputBar.requestFocus(-1);
                }
            }, 300);
        } else {
            inputBar.getInputBarView().setVisibility(View.VISIBLE);
            inputBar.requestFocus(-1);
        }
        inputBar.getInputBarView().setTag(itemData);
    }

    @Override
    public void inputBarKeyboardChange(boolean isShow) {
        if (isInputBarInit)
            inputBar.getInputBarView().setVisibility(isShow ? View.VISIBLE : View.GONE);
        isInputBarInit = true;
        if (!isShow) {
            inputBar.clearText();
        }
    }

    @Override
    public void inputBarOnClickBtn(InputBar.EInputBarBtnType type) {

    }

    @Override
    public void inputBarOnClickSendBtn(String content, int parentCommentId) {
        inputBar.getInputBarView().setVisibility(View.GONE);
        MyCommentMeta myCommentMeta = (MyCommentMeta) inputBar.getInputBarView().getTag();

        getRequestManager().requestSendComment(myCommentMeta.getType(), content, myCommentMeta.getType_id(), myCommentMeta.getId());
    }

    @Override
    protected void onRequestCallBack(NetUtil.NetParams params, JsonData response) {
        super.onRequestCallBack(params, response);

        if (params instanceof NetConfigs.RequestSendComment) {//POST评论
            SendCommentEntity sendCommentEntity = QDJsonUtil.Json2Object(response.toString(), SendCommentEntity.class);
            displayToast(sendCommentEntity.getMeta().getMsg());
        }
    }
}
